package com.ehouse.connection;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import com.ehouse.Drivers;
import com.ehouse.Notifications;
import com.ehouse.RulesBL;

public class PetitionManager implements Runnable {
//	private Socket socket;
	private PrintWriter printer;
	private BufferedReader reader;

	private String sentence;
	private String[] command;
	private boolean stop;
	ConnectionManager cm;

	public PetitionManager(Socket socket) throws IOException {
//		this.socket = socket;
		printer = new PrintWriter(socket.getOutputStream());
		reader = new BufferedReader(new InputStreamReader(
				socket.getInputStream()));
		sentence = "";
		stop = false;
		cm = new ConnectionManager();
	}

	public void run() {
		while (!stop) {
			try {
				char[] inBuffer = new char[1024];
				int read = reader.read(inBuffer);
				StringBuffer sb = new StringBuffer(read);
				for (int i = 0; i < read; i++) {
					sb.append(inBuffer[i]);
				}
				sentence = sb.toString();

				System.out.println(sentence);
				command = sentence.split("/");

				if (command[0].startsWith("GET_RULES_LIGHTS")) {

					String response = "RULES_LIGHT/";
					response += RulesBL.getInstance().getLightRules();
					System.out.println("Response: " + response);
					printer.print(response);
					printer.flush();
				} else if (command[0].startsWith("SET_RULES_LIGHTS")) {
					try {
						String rules = sentence
								.substring(sentence.indexOf('/') + 1);
						RulesBL.getInstance().setLightRules(rules);

						printer.print("CORRECT");
						printer.flush();
					} catch (Exception ex) {
						printer.print("ERROR");
						printer.flush();
					}
				} else if (command[0].startsWith("DELETE_RULES_LIGHTS")) {
					try {
						String guid = command[1];
						RulesBL.getInstance().deleteLightRule(guid);

						printer.print("CORRECT");
						printer.flush();
					} catch (Exception ex) {
						printer.print("ERROR");
						printer.flush();
					}
				} else if (command[0].startsWith("GET_RULES_IRRIGATION")) {
					String response = "RULES_IRRIGATION/";
					response += RulesBL.getInstance().getIrrigationRules();
					
					printer.print(response);
					printer.flush();
				} else if (command[0].startsWith("SET_RULES_IRRIGATION")) {
					try {
						String rules = sentence
								.substring(sentence.indexOf('/') + 1);
						RulesBL.getInstance().setIrrigationRules(rules);
						
						printer.print("CORRECT");
						printer.flush();
					} catch (Exception ex) {
						printer.print("ERROR");
						printer.flush();
					}
				} else if (command[0].startsWith("DELETE_RULES_IRRIGATION")) {
					try {
						String guid = command[1];
						RulesBL.getInstance().deleteIrrigationRule(guid);

						printer.print("CORRECT");
						printer.flush();
					} catch (Exception ex) {
						printer.print("ERROR");
						printer.flush();
					}
				} else if (command[0].startsWith("GET_RULES_TEMPERATURE")) {
					String response = "RULES_TEMPERATURE/";
					response += RulesBL.getInstance().getTempRule();
					printer.print(response);
					printer.flush();
				} else if (command[0].equals("SET_RULES_TEMPERATURE")) {
					try {
						String rules = sentence
								.substring(sentence.indexOf('/') + 1);
						RulesBL.getInstance().setTempRule(rules);

						printer.print("CORRECT");
						printer.flush();
					} catch (Exception ex) {
						printer.print("ERROR");
						printer.flush();
					}
				} else if (command[0].startsWith("DELETE_RULES_TEMPERATURE")) {
					try {
						RulesBL.getInstance().deleteTempRule();

						printer.print("CORRECT");
						printer.flush();
					} catch (Exception ex) {
						printer.print("ERROR");
						printer.flush();
					}
				} else if (command[0].startsWith("SEND_NOTIFICATION")) {
					String msg = command[1];
					try {
						Notifications.getInstance().sendNotifications(msg);
						printer.print("CORRECT");
						printer.flush();
					} catch (Exception ex) {
						printer.print("ERROR");
						printer.flush();
					}
				} else if (command[0].equals("REG_ID")) {
					String regID = sentence.substring(sentence.indexOf('/') + 1);
					if (regID.toLowerCase().indexOf("http") == -1)
						Drivers.getInstance().addAndroidRegID(regID);
					else
						Drivers.getInstance().addWindowsPhoneUri(regID);

					printer.print("CORRECT");
					printer.flush();
				} else {
					// byte[] responseBytes = cm.send(sentence).getBytes();
					if (command[0].equals("QUIT"))
						stop = true;
					String response = cm.send(sentence);
					System.out.println(response);
					printer.print(response);
					printer.flush();
				}

				// if (command[0].equals("HELLO_SERVER")) {
				// byte[] responseBytes = ("HELLO " + socket
				// .getLocalSocketAddress().toString()).getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// } else if (command[0].equals("REG_ID")) {
				// String regID = command[1];
				// if (regID.indexOf("http") == -1)
				// Drivers.getInstance().addAndroidRegID(regID);
				// else
				// Drivers.getInstance().addWindowsPhoneUri(regID);
				//
				// byte[] responseBytes = "CORRECT".getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// } else if (command[0].equals("GET_LIGHTS")) {
				//
				// // String response = "LIGHTS/";
				// // response += "Kitchen/";
				// // response +=
				// //
				// Drivers.getInstance().led_kitchen.Read().ToString().ToUpper();
				// // response += "/Living room/";
				// // response +=
				// //
				// Drivers.getInstance().led_living.Read().ToString().ToUpper();
				// // response += "/Bedroom/";
				// // response +=
				// //
				// Drivers.getInstance().led_bedroom.Read().ToString().ToUpper();
				// // response += "/Bathroom/";
				// // response +=
				// //
				// Drivers.getInstance().led_bathroom.Read().ToString().ToUpper();
				//
				// byte[] responseBytes = cm.getLights().getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// } else if (command[0].equals("SET_LIGHTS")) {
				//
				// try {
				// String room = command[1];
				// boolean state = command[2] == "TRUE";
				//
				// byte[] responseBytes = cm.switchSensor("lights", room,
				// state).getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// } catch (Exception ex) {
				// byte[] responseBytes = "ERROR".getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// }
				// } else if (command[0].equals("GET_TEMPERATURE")) {
				// byte[] responseBytes = cm.getTemperature().getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// } else if (command[0].equals("SET_TEMPERATURE")) {
				// String sensor = command[1];
				// boolean state = command[2] == "TRUE";
				//
				// try {
				// byte[] responseBytes = cm.switchSensor("temperature",
				// sensor, state).getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// } catch (Exception ex) {
				// byte[] responseBytes = "ERROR".getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// }
				// } else if (command[0].equals("SET_IRRIGATION")) {
				// boolean state = command[1] == "TRUE";
				//
				// try {
				//
				// byte[] responseBytes = cm.switchSensor("irrigation",
				// "", state).getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// } catch (Exception ex) {
				// byte[] responseBytes = "ERROR".getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// }
				//
				// } else if (command[0].equals("GET_WINDOWS")) {
				// byte[] responseBytes = cm.getWindows().getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// } else if (command[0].equals("GET_IRRIGATION")) {
				//
				// byte[] responseBytes = cm.getIrrigation().getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// } else if (command[0].equals("GET_RULES_LIGHTS")) {
				//
				// String response = "RULES_LIGHT/";
				// response += RulesBL.getInstance().getLightRules();
				// byte[] responseBytes = response.getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// } else if (command[0].equals("SET_RULES_LIGHTS")) {
				// try {
				// String rules = sentence
				// .substring(sentence.indexOf('/') + 1);
				// RulesBL.getInstance().setLightRules(rules);
				//
				// byte[] responseBytes = "CORRECT".getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// } catch (Exception ex) {
				// byte[] responseBytes = "ERROR".getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// }
				// } else if (command[0].equals("DELETE_RULES_LIGHTS")) {
				// try {
				// String guid = command[1];
				// RulesBL.getInstance().deleteLightRule(guid);
				//
				// byte[] responseBytes = "CORRECT".getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// } catch (Exception ex) {
				// byte[] responseBytes = "ERROR".getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// }
				// } else if (command[0].equals("GET_RULES_IRRIGATION")) {
				// String response = "RULES_IRRIGATION/";
				// response += RulesBL.getInstance().getIrrigationRules();
				// byte[] responseBytes = response.getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// } else if (command[0].equals("SET_RULES_IRRIGATION")) {
				// try {
				// String rules = sentence
				// .substring(sentence.indexOf('/') + 1);
				// RulesBL.getInstance().setIrrigationRules(rules);
				// byte[] responseBytes = "CORRECT".getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// } catch (Exception ex) {
				// byte[] responseBytes = "ERROR".getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// }
				// } else if (command[0].equals("DELETE_RULES_IRRIGATION")) {
				// try {
				// String guid = command[1];
				// RulesBL.getInstance().deleteIrrigationRule(guid);
				//
				// byte[] responseBytes = "CORRECT".getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// } catch (Exception ex) {
				// byte[] responseBytes = "ERROR".getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// }
				// } else if (command[0].equals("GET_RULES_TEMPERATURE")) {
				// String response = "RULES_TEMPERATURE/";
				// response += RulesBL.getInstance().getTempRule();
				// byte[] responseBytes = response.getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// } else if (command[0].equals("SET_RULES_TEMPERATURE")) {
				// try {
				// String rules = sentence
				// .substring(sentence.indexOf('/') + 1);
				// RulesBL.getInstance().setTempRule(rules);
				//
				// byte[] responseBytes = "CORRECT".getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// } catch (Exception ex) {
				// byte[] responseBytes = "ERROR".getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// }
				// } else if (command[0].equals("DELETE_RULES_TEMPERATURE")) {
				// try {
				// RulesBL.getInstance().deleteTempRule();
				//
				// byte[] responseBytes = "CORRECT".getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// } catch (Exception ex) {
				// byte[] responseBytes = "ERROR".getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// }
				// } else if (command[0].equals("SEND_NOTIFICATION")) {
				// String msg = command[1];
				// try {
				// Notifications.getInstance().sendNotifications(msg);
				// byte[] responseBytes = "CORRECT".getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// } catch (Exception ex) {
				// byte[] responseBytes = "ERROR".getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// }
				//
				// } else if (command[0].equals("QUIT")) {
				// byte[] responseBytes = "QUIT".getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// stop = true;
				// } else {
				// byte[] responseBytes = "ERROR".getBytes();
				// printer.print(responseBytes);
				// printer.flush();
				// System.out.println("Command Error: " + command[0]);
				// }

			} catch (Exception ex) {
				System.out.println("Error in connection");
				ex.printStackTrace();
				stop = true;
			}
		}
	}
}